using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace ASPNETVN.PORTAL.Modules.NEWS.Components
{
    public partial class Comment
    {
        #region Private members.

        protected int _ID;
        protected int _ItemID;
        protected string _Content = string.Empty;
        protected string _CreatedBy = string.Empty;
        protected DateTime _CreationTime;

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Properties.

        public int ID
        {
            set { this._ID = value; }
            get { return this._ID; }
        }

        public int ItemID
        {
            set { this._ItemID = value; }
            get { return this._ItemID; }
        }

        public string Content
        {
            set { this._Content = value; }
            get { return this._Content; }
        }

        public string CreatedBy
        {
            set { this._CreatedBy = value; }
            get { return this._CreatedBy; }
        }

        public DateTime CreationTime
        {
            set { this._CreationTime = value; }
            get { return this._CreationTime; }
        }


        #endregion

        //---------------------------------------------------------------------------------------------

        #region Select methods.

        public static Comment Load(int iD)
        {
            string spName = "[dbo].p_NEWS_Comment_Load";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, iD);
            Comment entity = null;
            IDataReader reader = db.ExecuteReader(dbCommand);
            if (reader.Read())
            {
                entity = new Comment();
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt32(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("ItemID"))) entity.ItemID = reader.GetInt32(reader.GetOrdinal("ItemID"));
                if (!reader.IsDBNull(reader.GetOrdinal("Content"))) entity.Content = reader.GetString(reader.GetOrdinal("Content"));
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedBy"))) entity.CreatedBy = reader.GetString(reader.GetOrdinal("CreatedBy"));
                if (!reader.IsDBNull(reader.GetOrdinal("CreationTime"))) entity.CreationTime = reader.GetDateTime(reader.GetOrdinal("CreationTime"));
            }
            reader.Close();
            dbCommand.Connection.Close();

            return entity;
        }

        //---------------------------------------------------------------------------------------------

        public static DataSet SelectBy_ItemID(int itemID)
        {
            string spName = "[dbo].p_NEWS_Comment_SelectBy_ItemID";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ItemID", SqlDbType.Int, itemID);

            return db.ExecuteDataSet(dbCommand);
        }
        //---------------------------------------------------------------------------------------------

        public static DataSet SelectAll()
        {
            string spName = "[dbo].p_NEWS_Comment_SelectAll";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);


            return db.ExecuteDataSet(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static IDataReader SelectReaderAll()
        {
            string spName = "[dbo].p_NEWS_Comment_SelectAll";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);


            return db.ExecuteReader(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static DataSet SelectDynamic(string whereCondition, string orderByExpression)
        {
            string spName = "[dbo].p_NEWS_Comment_SelectDynamic";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);

            return db.ExecuteDataSet(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static IDataReader SelectReaderDynamic(string whereCondition, string orderByExpression)
        {
            string spName = "[dbo].p_NEWS_Comment_SelectDynamic";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);

            return db.ExecuteReader(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Insert methods.

        public static int Insert(int itemID, string content, string createdBy, DateTime creationTime)
        {
            Comment entity = new Comment();
            entity.ItemID = itemID;
            entity.Content = content;
            entity.CreatedBy = createdBy;
            entity.CreationTime = creationTime;
            return entity.Insert();
        }

        public int Insert()
        {
            return this.Insert(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Insert(SqlTransaction transaction)
        {
            string spName = "[dbo].p_NEWS_Comment_Insert";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddOutParameter(dbCommand, "@ID", SqlDbType.Int, 4);
            db.AddInParameter(dbCommand, "@ItemID", SqlDbType.Int, this._ItemID);
            db.AddInParameter(dbCommand, "@Content", SqlDbType.NText, this._Content);
            db.AddInParameter(dbCommand, "@CreatedBy", SqlDbType.NVarChar, this._CreatedBy);
            db.AddInParameter(dbCommand, "@CreationTime", SqlDbType.DateTime, this._CreationTime);

            if (transaction != null)
            {
                db.ExecuteNonQuery(dbCommand, transaction);
                this._ID = (int)db.GetParameterValue(dbCommand, "@ID");
                return this._ID;
            }
            else
            {
                db.ExecuteNonQuery(dbCommand);
                this._ID = (int)db.GetParameterValue(dbCommand, "@ID");
                return this._ID;
            }
        }

        //---------------------------------------------------------------------------------------------

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Update methods.

        public static int Update(int iD, int itemID, string content, string createdBy, DateTime creationTime)
        {
            Comment entity = new Comment();
            entity.ID = iD;
            entity.ItemID = itemID;
            entity.Content = content;
            entity.CreatedBy = createdBy;
            entity.CreationTime = creationTime;
            return entity.Update();
        }

        public int Update()
        {
            return this.Update(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Update(SqlTransaction transaction)
        {
            string spName = "[dbo].p_NEWS_Comment_Update";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, this._ID);
            db.AddInParameter(dbCommand, "@ItemID", SqlDbType.Int, this._ItemID);
            db.AddInParameter(dbCommand, "@Content", SqlDbType.NText, this._Content);
            db.AddInParameter(dbCommand, "@CreatedBy", SqlDbType.NVarChar, this._CreatedBy);
            db.AddInParameter(dbCommand, "@CreationTime", SqlDbType.DateTime, this._CreationTime);

            if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Delete methods.

        public static int Delete(int iD)
        {
            Comment entity = new Comment();
            entity.ID = iD;
            return entity.Delete();
        }

        public int Delete()
        {
            return this.Delete(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Delete(SqlTransaction transaction)
        {
            string spName = "[dbo].p_NEWS_Comment_Delete";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, this._ID);

            if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
        }

        #endregion
    }
}